/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.camachosanchez.reportes;

import com.camachosanchez.controler.jdbc.NeumaticosJdbcControler;
import com.itextpdf.awt.DefaultFontMapper;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
import java.awt.Desktop;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Formatter;
import java.util.List;
import javax.swing.JOptionPane;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 *
 * @author Zully
 */
public class ReporteRendimientoMarcas {
    
    private Formatter fmt;
    
    public ReporteRendimientoMarcas() {
        fmt = new Formatter();
    }
    
    public void generarReporte(int anio, int mes) {
        String nombrePdf = "reportes/reencauchesXMarca_" + fmt.format("%02d",mes) + "-" + anio + ".pdf";
        getChartToPDF(generarBarChart(anio,mes), 400, 300, nombrePdf);
        File pdfFile = new File(nombrePdf);
        if (Desktop.isDesktopSupported()) {
            try {
                Desktop.getDesktop().open(pdfFile);
            } catch (IOException ex) {
                System.out.print(ex.getMessage());
            }
        } else {
                JOptionPane.showMessageDialog(null, "No se puede abrir el reporte.", "Error al abrir el archivo", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private JFreeChart generarBarChart(int anio, int mes) {
        NeumaticosJdbcControler dao_datos = new NeumaticosJdbcControler();
        List<Object[]> datos = dao_datos.reencauchesXMarca(anio, mes);
        DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
        for(Object[] dato : datos) {
            dataSet.setValue(Double.parseDouble(dato[1].toString()), "Reencauches", dato[2].toString() + "(" + dato[0]+")");
        }
        JFreeChart chart = ChartFactory.createBarChart(
                "Reencauches por Marca", "Marca", "Promedio de Reencauches",
                dataSet, PlotOrientation.VERTICAL, false, true, false);
        return chart;
    }
    
    private void getChartToPDF(JFreeChart chart, int width, int height, String fileName) {
        PdfWriter writer;
        Document document = new Document();
        try {
            writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
            document.open();
            PdfContentByte contentByte = writer.getDirectContent();
            PdfTemplate template = contentByte.createTemplate(width, height);
            Graphics2D graphics2d = template.createGraphics(width, height,
                    new DefaultFontMapper());
            Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width,
                    height);
            chart.draw(graphics2d, rectangle2d);

            graphics2d.dispose();
            contentByte.addTemplate(template, 100, 450);
        } catch (Exception e) {
            System.out.print(e.getMessage());
        }
        document.close();
    }
}
